package fun._6742.model.valueobject;

import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

@Data
public class CartVO {
    private List<CartItemVO> cartItems;
    /**
     * 商品总件数
     */
    private Integer totalNum=0;
    /**
     * 总价格
     */
    private BigDecimal totalPrice= BigDecimal.valueOf(0);
    /**
     * 实际支付总价格
     */
    private BigDecimal realPayPrice= BigDecimal.valueOf(0);

    public CartVO(){}

    public CartVO(List<CartItemVO> cartItems){
        this.setCartItems(cartItems);
        this.totalNum = getTotalNum();
        this.realPayPrice  = getRealPayPrice();
        this.totalPrice = getTotalPrice();
    }

    public Integer getTotalNum() {
        if (this.cartItems!=null){
            int num = cartItems.stream().mapToInt(CartItemVO::getProductNum).sum();
            return num;
        }
        return 0;
    }

    public BigDecimal getTotalPrice() {
        if (this.cartItems!=null){
            BigDecimal sum = new BigDecimal(0);
            for (CartItemVO item:cartItems){
                sum = sum.add(item.getTotalPrice());
            }
            return sum;
        }
        return totalPrice;
    }

    public BigDecimal getRealPayPrice() {
        if (this.cartItems!=null){
            BigDecimal sum = new BigDecimal(0);
            for (CartItemVO item:cartItems){
                sum = sum.add(item.getTotalPrice());
            }
            return sum;
        }
        return totalPrice;
    }

    public List<CartItemVO> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItemVO> cartItems) {
        this.cartItems = cartItems;
        this.totalNum = getTotalNum();
        this.realPayPrice  = getRealPayPrice();
        this.totalPrice = getTotalPrice();
    }
}
